<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
    import defaultUserImg from 'assets/images/use-default-img.png';
    import positionIcon from 'assets/images/position-icon.png';
    import smileFace0 from 'assets/images/smile_face_0.png';
    import smileFace1 from 'assets/images/smile_face_1.png';
    import smileFace2 from 'assets/images/smile_face_2.png';
    import api from 'api/api';
    import modal from 'utils/modal';

    const sortTabs = [
        {
            type: 0,
            title: '好评'
        },
        {
            type: 1,
            title: '中评'
        },
        {
            type: 2,
            title: '差评'
        }
    ];

    function vendorData(data, vm){
        data = data || {};
        vm.rateList = data.rate_arr || [];
        if(vm.rateList.length < vm.limit) vm.listCompleted = true;
    }

    export default{
        data(){
            return{
                defaultUserImg: defaultUserImg,
                smileFace0: smileFace0,
                smileFace1: smileFace1,
                smileFace2: smileFace2,
                positionIcon: positionIcon,
                sortTabs: sortTabs,

                //接口参数
                type: 0, //默认请求好评
                start: 0, // 起始位置
                limit: 8, //默认页面个数
                goodsId: null, //商品编号ID

                rateList: [], //评价列表
                pageLoaded: false,//列表加载是否完成
                listCompleted: false, //列表加载完成
                loading: false //记载列表
            }
        },
        created(){
            let self = this;
            let bundleUrl = weex.config.bundleUrl;
            bundleUrl = bundleUrl.split("goodsId=")[1];
            if(!bundleUrl) {
                modal.toast("该商品不存在");
                return false;
            }
            self.goodsId = bundleUrl;
            this.get();
        },
        methods: {
            /**页面初始化*/
            get(){
                let self = this;
                api.sendHttp('goodsRateApi', {
                    id: self.goodsId,
                    start: self.start,
                    limit: self.limit,
                    type: self.type
                }).then(res => {
                    self.pageLoaded = true;
                    vendorData(res.data, self);
                }, res => {
                    self.pageLoaded = true;
                    modal.toast(res.msg || '获取商品评价失败');
                });
            },

            /**分页获取更多*/
            fetchMore(e) {
                this.$refs['list'].resetLoadmore(); //下一次滚动时强制触发loadmore

                let self = this;
                if(self.listCompleted) {
                    return false;
                }
                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                api.sendHttp('goodsRateApi', {
                    id: self.goodsId,
                    start: self.start,
                    limit: self.limit,
                    type: self.type
                }).then(res => {
                    self.loading = false;
                    let data = res.data || {};
                    let _rateList = data.rate_arr || [];
                    if(_rateList.length < self.limit) self.listCompleted = true;
                    self.rateList = self.rateList.concat(_rateList);
                }, res => {
                    self.loading = false;
                    modal.toast(res.msg || '获取商品评价失败');
                });
            },

            /**分类获取不同评价*/
            sortClick(s) {
                let self = this;
                if(s && s.type == self.type) return false;
                self.type = s.type;

                this.pageLoaded = false;
                this.rateList.length = 0;//清空数组
                this.listCompleted = false;
                this.loading = false;
                this.start = 0;
                this.get();
            }
        }
    }
</script>
